package com.genius.dingding.util;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request.BtnJsonList;
import com.dingtalk.api.request.OapiMessageCorpconversationGetsendprogressRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationGetsendresultRequest;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiMessageCorpconversationGetsendprogressResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationGetsendresultResponse;
import com.taobao.api.ApiException;


/**
 * 会话API
 *
 * https://open-doc.dingtalk.com/docs/doc.htm?treeId=371&articleId=106817&docType=1
 */
public class MessageHelper {

    /**
     *  创建发送通知（新SDK）link
     */
    public static Long createLink(long agentId, String users, 
    		String deps,String messageUrl,String picUrl,String title,String text) throws Exception {

    	DingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/topapi/message/corpconversation/asyncsend_v2");

    	OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
    	req.setAgentId(agentId);
    	if(users != null){
    		req.setUseridList(users);
    	}
    	if(deps != null){
    		req.setDeptIdList(deps);
    	}
    	req.setToAllUser(false);
    	OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
    	msg.setMsgtype("link");
    	msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
    	msg.getLink().setTitle(title);
    	msg.getLink().setText(text);
    	msg.getLink().setMessageUrl(messageUrl);
    	msg.getLink().setPicUrl(picUrl);
    	req.setMsg(msg);
    	OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, GetDingDing.getInstance().getToken());
    	Long task_id = rsp.getTaskId();
    	
    	return task_id;
    }
    /**
     *  创建发送通知（新SDK）link
     */
    public static Long createLink(long agentId, String users, 
    		String deps,String messageUrl,String picUrl,String title,String text,String accessToken) throws Exception {

    	DingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/topapi/message/corpconversation/asyncsend_v2");

    	OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
    	req.setAgentId(agentId);
    	if(users != null){
    		req.setUseridList(users);
    	}
    	if(deps != null){
    		req.setDeptIdList(deps);
    	}
    	req.setToAllUser(false);
    	OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
    	msg.setMsgtype("link");
    	msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
    	msg.getLink().setTitle(title);
    	msg.getLink().setText(text);
    	msg.getLink().setMessageUrl(messageUrl);
    	msg.getLink().setPicUrl(picUrl);
    	req.setMsg(msg);
    	OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, accessToken);
    	Long task_id = rsp.getTaskId();
    	
    	return task_id;
    }
    /**
     *  创建发送通知（新SDK）text
     */
    public static Long createTextNew(long agentId, String users, 
    		String deps,String text) throws Exception {

    	DingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/topapi/message/corpconversation/asyncsend_v2");

    	OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
    	req.setAgentId(agentId);
    	if(users != null){
    		req.setUseridList(users);
    	}
    	if(deps != null){
    		req.setDeptIdList(deps);
    	}
    	req.setToAllUser(false);
    	OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
    	msg.setMsgtype("text");
    	msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
    	msg.getText().setContent(text);
    	req.setMsg(msg);
    	OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, GetDingDing.getInstance().getToken());
    	Long task_id = rsp.getTaskId();
    	
    	return task_id;
    }
    /**
     *  创建发送通知（新SDK）text
     */
    public static Long createTextNewHaveToken(long agentId, String users, 
    		String deps,String text,String token) throws Exception {

    	DingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/topapi/message/corpconversation/asyncsend_v2");

    	OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
    	req.setAgentId(agentId);
    	if(users != null){
    		req.setUseridList(users);
    	}
    	if(deps != null){
    		req.setDeptIdList(deps);
    	}
    	req.setToAllUser(false);
    	OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
    	msg.setMsgtype("text");
    	msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
    	msg.getText().setContent(text);
    	req.setMsg(msg);
    	OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, token);
    	Long task_id = rsp.getTaskId();
//    	System.out.println(rsp.getBody());
    	return task_id;
    }
    //查询工作通知消息的发送进度
    public static OapiMessageCorpconversationGetsendprogressResponse getSendProgress(long agentId,long taskId) throws ApiException{
    	DingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/topapi/message/corpconversation/getsendprogress");
    	OapiMessageCorpconversationGetsendprogressRequest request  = new OapiMessageCorpconversationGetsendprogressRequest();
    	request.setAgentId(agentId);
    	request.setTaskId(taskId);
    	OapiMessageCorpconversationGetsendprogressResponse response = client.execute(request, GetDingDing.getInstance().getToken());
		return response;
    }
  //查询工作通知消息的发送结果
    public static OapiMessageCorpconversationGetsendresultResponse getSendResult(long agentId,long taskId) throws ApiException{
    	DingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/topapi/message/corpconversation/getsendresult");
    	OapiMessageCorpconversationGetsendresultRequest request  = new OapiMessageCorpconversationGetsendresultRequest();
    	request.setAgentId(agentId);
    	request.setTaskId(taskId);
    	OapiMessageCorpconversationGetsendresultResponse response = client.execute(request, GetDingDing.getInstance().getToken());
		return response;
    }
  //查询工作通知消息的发送结果
    public static OapiMessageCorpconversationGetsendresultResponse getSendResultHaveToken(long agentId,long taskId,String token) throws ApiException{
    	DingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/topapi/message/corpconversation/getsendresult");
    	OapiMessageCorpconversationGetsendresultRequest request  = new OapiMessageCorpconversationGetsendresultRequest();
    	request.setAgentId(agentId);
    	request.setTaskId(taskId);
    	OapiMessageCorpconversationGetsendresultResponse response = client.execute(request, token);
		return response;
    }
    /**
     *  创建发送通知 markdown
     */
    public static Long createMarkdown(long agentId, String users, 
    		String deps,String title, String text,String accessToken) throws Exception {

    	DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");

    	OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
    	req.setAgentId(agentId);
    	if(users != null){
    		req.setUseridList(users);
    	}
    	if(deps != null){
    		req.setDeptIdList(deps);
    	}
    	req.setToAllUser(false);
    	OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
    	msg.setMsgtype("markdown");
    	msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
    	msg.getMarkdown().setText(text);
    	msg.getMarkdown().setTitle(title);
    	req.setMsg(msg);
    	OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, GetDingDing.getInstance().getToken());
    	Long task_id = rsp.getTaskId();
    	
    	return task_id;
    }
    /**
     *  创建发送通知 markdown
     */
    public static JSONObject createMarkdownJson(long agentId, String users, 
    		String deps,String title, String text,String accessToken) throws Exception {

    	DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");

    	OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
    	req.setAgentId(agentId);
    	if(users != null){
    		req.setUseridList(users);
    	}
    	if(deps != null){
    		req.setDeptIdList(deps);
    	}
    	req.setToAllUser(false);
    	OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
    	msg.setMsgtype("markdown");
    	msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
    	msg.getMarkdown().setText(text);
    	msg.getMarkdown().setTitle(title);
    	req.setMsg(msg);
    	OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, GetDingDing.getInstance().getToken());
//    	Long task_id = rsp.getTaskId();
    	
    	return JSONObject.parseObject(rsp.getBody());
    }
    /**
     *  创建发送通知 卡片消息 ActionCard
     *  single_title 使用整体跳转ActionCard样式时的标题，必须与single_url同时设置，最长20个字符
     *  single_url 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接，最长500个字符
     *  btn_orientation 使用独立跳转ActionCard样式时的按钮排列方式，竖直排列(0)，横向排列(1)；必须与btn_json_list同时设置
     *  btn_json_list 使用独立跳转ActionCard样式时的按钮列表；必须与btn_orientation同时设置
     *  btn_json_list.title 使用独立跳转ActionCard样式时的按钮的标题，最长20个字符
     *  btn_json_list.action_url 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接，最长500个字符
     */
    public static Long createActionCard(long agentId, String users, 
    		String deps,String title,String markdown, String single_title, String single_url
    		, String btn_orientation, List<BtnJsonList> btn_json_list) throws Exception {

    	DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");

    	OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
    	req.setAgentId(agentId);
    	if(users != null){
    		req.setUseridList(users);
    	}
    	if(deps != null){
    		req.setDeptIdList(deps);
    	}
    	req.setToAllUser(false);
    	OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
    	msg.setMsgtype("action_card");
    	msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
    	msg.getActionCard().setTitle(title);
    	msg.getActionCard().setMarkdown(markdown);
    	if(single_title != null && !single_title.equals("null") && !single_title.equals("")){
    		msg.getActionCard().setSingleTitle(single_title);
    	}
    	if(single_url != null && !single_url.equals("null") && !single_url.equals("")){
    		msg.getActionCard().setSingleUrl(single_url);
    	}
    	if(btn_json_list != null && btn_json_list.size() > 0){
    		msg.getActionCard().setBtnJsonList(btn_json_list);
    		msg.getActionCard().setBtnOrientation(btn_orientation);
    	}
    	
    	req.setMsg(msg);
    	OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, GetDingDing.getInstance().getToken());
    	Long task_id = rsp.getTaskId();
    	
    	return task_id;
    }
    /**
     *  创建发送通知 卡片消息 ActionCard
     *  single_title 使用整体跳转ActionCard样式时的标题，必须与single_url同时设置，最长20个字符
     *  single_url 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接，最长500个字符
     *  btn_orientation 使用独立跳转ActionCard样式时的按钮排列方式，竖直排列(0)，横向排列(1)；必须与btn_json_list同时设置
     *  btn_json_list 使用独立跳转ActionCard样式时的按钮列表；必须与btn_orientation同时设置
     *  btn_json_list.title 使用独立跳转ActionCard样式时的按钮的标题，最长20个字符
     *  btn_json_list.action_url 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接，最长500个字符
     */
    public static JSONObject createActionCardJson(long agentId, String users, 
    		String deps,String title,String markdown, String single_title, String single_url
    		, String btn_orientation, List<BtnJsonList> btn_json_list) throws Exception {

    	DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");

    	OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
    	req.setAgentId(agentId);
    	if(users != null){
    		req.setUseridList(users);
    	}
    	if(deps != null){
    		req.setDeptIdList(deps);
    	}
    	req.setToAllUser(false);
    	OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
    	msg.setMsgtype("action_card");
    	msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
    	msg.getActionCard().setTitle(title);
    	msg.getActionCard().setMarkdown(markdown);
    	if(single_title != null && !single_title.equals("null") && !single_title.equals("")){
    		msg.getActionCard().setSingleTitle(single_title);
    	}
    	if(single_url != null && !single_url.equals("null") && !single_url.equals("")){
    		msg.getActionCard().setSingleUrl(single_url);
    	}
    	if(btn_json_list != null && btn_json_list.size() > 0){
    		msg.getActionCard().setBtnJsonList(btn_json_list);
    		msg.getActionCard().setBtnOrientation(btn_orientation);
    	}
    	
    	req.setMsg(msg);
    	OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, GetDingDing.getInstance().getToken());
//    	Long task_id = rsp.getTaskId();
    	System.out.println(rsp.getBody());
    	return JSONObject.parseObject(rsp.getBody());
    }
    /**
     *  创建发送通知 卡片消息 ActionCard
     *  single_title 使用整体跳转ActionCard样式时的标题，必须与single_url同时设置，最长20个字符
     *  single_url 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接，最长500个字符
     *  btn_orientation 使用独立跳转ActionCard样式时的按钮排列方式，竖直排列(0)，横向排列(1)；必须与btn_json_list同时设置
     *  btn_json_list 使用独立跳转ActionCard样式时的按钮列表；必须与btn_orientation同时设置
     *  btn_json_list.title 使用独立跳转ActionCard样式时的按钮的标题，最长20个字符
     *  btn_json_list.action_url 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接，最长500个字符
     */
    public static Long createActionCard(long agentId, String users, 
    		String deps,String title,String markdown, String single_title, String single_url
    		, String btn_orientation, List<BtnJsonList> btn_json_list,String accessToken) throws Exception {

    	DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");

    	OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
    	req.setAgentId(agentId);
    	if(users != null){
    		req.setUseridList(users);
    	}
    	if(deps != null){
    		req.setDeptIdList(deps);
    	}
    	req.setToAllUser(false);
    	OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
    	msg.setMsgtype("action_card");
    	msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
    	msg.getActionCard().setTitle(title);
    	msg.getActionCard().setMarkdown(markdown);
    	if(single_title != null && !single_title.equals("null") && !single_title.equals("")){
    		msg.getActionCard().setSingleTitle(single_title);
    	}
    	if(single_url != null && !single_url.equals("null") && !single_url.equals("")){
    		msg.getActionCard().setSingleUrl(single_url);
    	}
    	if(btn_json_list != null && btn_json_list.size() > 0){
    		msg.getActionCard().setBtnJsonList(btn_json_list);
    		msg.getActionCard().setBtnOrientation(btn_orientation);
    	}
    	
    	req.setMsg(msg);
    	OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, accessToken);
    	Long task_id = rsp.getTaskId();
    	System.out.println(rsp.getBody());
    	return task_id;
    }
    /**
     *  创建发送通知 卡片消息 ActionCard
     *  single_title 使用整体跳转ActionCard样式时的标题，必须与single_url同时设置，最长20个字符
     *  single_url 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接，最长500个字符
     *  btn_orientation 使用独立跳转ActionCard样式时的按钮排列方式，竖直排列(0)，横向排列(1)；必须与btn_json_list同时设置
     *  btn_json_list 使用独立跳转ActionCard样式时的按钮列表；必须与btn_orientation同时设置
     *  btn_json_list.title 使用独立跳转ActionCard样式时的按钮的标题，最长20个字符
     *  btn_json_list.action_url 消息点击链接地址，当发送消息为小程序时支持小程序跳转链接，最长500个字符
     */
    public static JSONObject createActionCardJson(long agentId, String users, 
    		String deps,String title,String markdown, String single_title, String single_url
    		, String btn_orientation, List<BtnJsonList> btn_json_list,String accessToken) throws Exception {

    	DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");

    	OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
    	req.setAgentId(agentId);
    	if(users != null){
    		req.setUseridList(users);
    	}
    	if(deps != null){
    		req.setDeptIdList(deps);
    	}
    	req.setToAllUser(false);
    	OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
    	msg.setMsgtype("action_card");
    	msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
    	msg.getActionCard().setTitle(title);
    	msg.getActionCard().setMarkdown(markdown);
    	if(single_title != null && !single_title.equals("null") && !single_title.equals("")){
    		msg.getActionCard().setSingleTitle(single_title);
    	}
    	if(single_url != null && !single_url.equals("null") && !single_url.equals("")){
    		msg.getActionCard().setSingleUrl(single_url);
    	}
    	if(btn_json_list != null && btn_json_list.size() > 0){
    		msg.getActionCard().setBtnJsonList(btn_json_list);
    		msg.getActionCard().setBtnOrientation(btn_orientation);
    	}
    	
    	req.setMsg(msg);
    	OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(req, accessToken);
//    	Long task_id = rsp.getTaskId();
    	System.out.println(rsp.getBody());
    	return JSONObject.parseObject(rsp.getBody());
    }
}
